/*
 * PGPEditor.java
 *
 * Created on 14 de Julho de 2011, 19:03
 */

package intopo.game.impl.pgp;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.event.DocumentEvent;

/**
 * Component that shows and allows the user to set the parameters of a <i>Public Good Provision</i> game.  The parameters are the value of the good, the cost to provide for the good, and the number of players.
 *
 * <p> The default parameters are good worth {@code 1}, provision costs {@code 0.6} and the number of players is 3.
 *
 * <p> The
 * @author  pedro
 */
public class PGPEditor 
	extends intopo.game.gui.AbstractGameEditorComponent
	<PublicGoodProvisionGame, PublicGoodProvisionStrategy>
{
	private int error;
	/** Creates new form BeanForm */
	public PGPEditor ()
	{
		error = 0;
		initComponents ();
		this.add (this.infoPanel, BorderLayout.SOUTH);
		this.updateInfoPanel (true);
//		Listener listener = new Listener ();
//		publicGoodTextField.getDocument ().addDocumentListener (listener);
//		provisionCostTextField.getDocument ().addDocumentListener (listener);
//		numberPlayersTextField.getDocument ().addDocumentListener (listener);
	}

	public String getConfigFileDescription ()
	{
		throw new Error ();
//		float provisionCost = Float.parseFloat (provisionCostTextField.getText ());
//		float publicGoodValue = Float.parseFloat (publicGoodTextField.getText ());
//		int numberPlayers = Integer.parseInt (numberPlayersTextField.getText ());
//
//		return String.format ("PublicGoodProvisionGame\n%f %f %d", provisionCost, publicGoodValue, numberPlayers);
	}
	public PublicGoodProvisionGame getGame ()
	{
		if (this.error == 0) {
			float provisionCost = (Float) this.provisionCostFormattedTextField.getValue ();
			float publicGoodValue = (Float) this.publicGoodFormattedTextField.getValue ();
			int numberPlayers = (Integer) this.numberPlayersFormattedTextField.getValue ();
			return new PublicGoodProvisionGame (provisionCost, publicGoodValue, numberPlayers);
		}
		else {
			return null;
		}
	}
	public void setGame (PublicGoodProvisionGame game)
	{
		this.game = game;
		this.provisionCostFormattedTextField.setValue (new Float (game.getProvisionCost ()));
		this.publicGoodFormattedTextField.setValue (new Float (game.getPublicGoodValue ()));
		this.numberPlayersFormattedTextField.setValue (new Integer (game.numberPlayers ()));
	}
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {
      java.awt.GridBagConstraints gridBagConstraints;

      javax.swing.JPanel parametersPanel = new javax.swing.JPanel();
      javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
      publicGoodFormattedTextField = new javax.swing.JFormattedTextField();
      javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
      provisionCostFormattedTextField = new javax.swing.JFormattedTextField();
      javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
      numberPlayersFormattedTextField = new javax.swing.JFormattedTextField();

      setBorder(javax.swing.BorderFactory.createTitledBorder("game parameters"));
      setLayout(new java.awt.BorderLayout());

      parametersPanel.setLayout(new java.awt.GridBagLayout());

      jLabel1.setText("Public good value: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      parametersPanel.add(jLabel1, gridBagConstraints);

      publicGoodFormattedTextField.setColumns(5);
      publicGoodFormattedTextField.setValue(new Float (1));
      publicGoodFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            publicGoodFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      parametersPanel.add(publicGoodFormattedTextField, gridBagConstraints);

      jLabel2.setText("Provision cost: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridy = 1;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      parametersPanel.add(jLabel2, gridBagConstraints);

      provisionCostFormattedTextField.setColumns(5);
      provisionCostFormattedTextField.setValue(new Float (0.5));
      provisionCostFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            provisionCostFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      parametersPanel.add(provisionCostFormattedTextField, gridBagConstraints);

      jLabel3.setText("Number of players: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridy = 2;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      parametersPanel.add(jLabel3, gridBagConstraints);

      numberPlayersFormattedTextField.setColumns(5);
      numberPlayersFormattedTextField.setValue(new Integer (3));
      numberPlayersFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            numberPlayersFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      parametersPanel.add(numberPlayersFormattedTextField, gridBagConstraints);

      add(parametersPanel, java.awt.BorderLayout.CENTER);
   }// </editor-fold>//GEN-END:initComponents

	private void publicGoodFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_publicGoodFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final float valueG = (Float) this.publicGoodFormattedTextField.getValue ();
			final float valueP = (Float) this.provisionCostFormattedTextField.getValue ();
			final boolean ok = valueG > 0 && valueG > valueP;
			this.update (this.publicGoodFormattedTextField, ok);
			if (ok) {
				error &= ~1;
				this.updateInfoPanel (true);
			}
			else {
				error |= 1;
			}
		}
	}//GEN-LAST:event_publicGoodFormattedTextFieldPropertyChange

	private void provisionCostFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_provisionCostFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final float valueP = (Float) this.provisionCostFormattedTextField.getValue ();
			final float valueG = (Float) this.publicGoodFormattedTextField.getValue ();
			final boolean ok = valueP > 0 && valueG > valueP;
			this.update (this.provisionCostFormattedTextField, ok);
			if (ok) {
				error &= ~2;
				this.updateInfoPanel (true);
			}
			else {
				error |= 2;
			}
		}
}//GEN-LAST:event_provisionCostFormattedTextFieldPropertyChange

	private void numberPlayersFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_numberPlayersFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final int value = (Integer) this.numberPlayersFormattedTextField.getValue ();
			final boolean ok = value >= 2;
			this.update (this.numberPlayersFormattedTextField, ok);
			if (ok) {
				error &= ~4;
				this.updateInfoPanel (true);
			}
			else {
				error |= 4;
			}
		}
}//GEN-LAST:event_numberPlayersFormattedTextFieldPropertyChange
	
	
   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JFormattedTextField numberPlayersFormattedTextField;
   private javax.swing.JFormattedTextField provisionCostFormattedTextField;
   private javax.swing.JFormattedTextField publicGoodFormattedTextField;
   // End of variables declaration//GEN-END:variables
	
//	/**
//	 * Inner class that implements the document listener used by the text fields of the enclosing class.
//	 */
//	private class Listener
//		implements
//			javax.swing.event.DocumentListener,
//			java.io.Serializable
//	{
//		public void changedUpdate (DocumentEvent e) {
//			updateParameter (e);
//		}
//		public void insertUpdate (DocumentEvent e) {
//			updateParameter (e);
//		}
//		public void removeUpdate (DocumentEvent e) {
//			updateParameter (e);
//		}
//		/**
//		 * Updates the parameter corresponding to the text field that caused the document event.
//		 */
//		private void updateParameter (DocumentEvent e)
//		{
//			javax.swing.JTextField tf = null;
//			try {
//				String parameterName;
//				Object oldValue, newValue;
//				if (e.getDocument () == provisionCostTextField.getDocument ()) {
//					tf = provisionCostTextField;
//					parameterName = "provision cost";
//					oldValue = game.getProvisionCost ();
//					game.setProvisionCost (Float.parseFloat (provisionCostTextField.getText ()));
//					newValue = game.getProvisionCost ();
//				}
//				else if (e.getDocument () == publicGoodTextField.getDocument ()) {
//					tf = publicGoodTextField;
//					parameterName = "";
//					oldValue = game.getPublicGoodValue ();
//					game.setPublicGoodValue (Float.parseFloat (publicGoodTextField.getText ()));
//					newValue = game.getPublicGoodValue ();
//				}
//				else if (e.getDocument () == numberPlayersTextField.getDocument ()) {
//					tf = numberPlayersTextField;
//					parameterName = "number players";
//					oldValue = game.numberPlayers ();
//					game.setNumberPlayers (Integer.parseInt (numberPlayersTextField.getText ()));
//					newValue = game.numberPlayers ();
//				}
//				else {
//					throw new Error ("Not reachable");
//				}
//				tf.setForeground (Color.BLACK);
//				fireParameterUpdated (parameterName, oldValue, newValue);
//			}
//			catch (NumberFormatException ex) {
//				tf.setForeground (Color.RED);
//			}
//			catch (IllegalArgumentException ex) {
//				tf.setForeground (Color.MAGENTA);
//			}
//		}
//	}
}
